﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PIM_ATE.Mods
{
    class ClsWebRecord : ClsDataResultForMOD
    {
        const string TFPID = "MOBI_FL_PIM_V1.6";
        const string PROJNAME = "TLRRU-DFL";
        const string PROJVER = "V0.1";
        const int PROCESSNUM = 1;

        string procode = "";
        List<PIMTestItemInfo> _lstPIMTestItem;
        ClsDataPIMTestForMOD _cPIMTest;

        List<tb_Param_DFL_Info> _lstParam;
        tb_Result_DFL_Info _cResult;
        InstrumentGroup _instr;
        public ClsWebRecord(object objInstr)
        {
            if (objInstr != null)
            {
                _instr = (InstrumentGroup)objInstr;
            }
        }
        public bool construct(bool m_isAllFail, DateTime timeStart, DateTime timeStop,
                                Dictionary<int, JcPIMRecordResult> dicResults,
                                List<ClsDataConfig> lstConifg,
                                ClsDataProduct productObj,
                                ClsDataUser userObj, ClsDataSetup setupObj)
        {
            procode = productObj.sProcode;
            _lstPIMTestItem = new List<PIMTestItemInfo>();
            _cPIMTest = new ClsDataPIMTestForMOD();

            _lstParam = new List<tb_Param_DFL_Info>();
            _cResult = new tb_Result_DFL_Info();

            string sItemresult = "";
            string sItemselinfo = "";

            try
            {
                for (int i = 0; i < lstConifg.Count; i++)
                {
                    if (dicResults.ContainsKey(i))
                    {
                        double f1 = 0, f2 = 0;
                        PIMFunction.formateSpec(lstConifg[i].SpecReg, ref f1, ref f2);

                        #region MOBI-LIST
                        var cPIMTestItem = new PIMTestItemInfo();
                        cPIMTestItem.ProductSN = productObj.sProductSN;
                        cPIMTestItem.TestTime = timeStop;
                        cPIMTestItem.TopoAlias = lstConifg[i].ProCH;

                        if (setupObj.IsCheckOn)
                            cPIMTestItem.TopoAlias += "@(" + lstConifg[i].Check + ")";

                        cPIMTestItem.InterceptOrder = lstConifg[i].PIMOrder;
                        JcPIMDataResults[] temp = dicResults[i].valus_knock == null ? dicResults[i].valus : dicResults[i].valus_knock;
                        cPIMTestItem.InputPower1 = (decimal)temp[dicResults[i].index].P1;
                        cPIMTestItem.InputPower2 = (decimal)temp[dicResults[i].index].P2;
                        cPIMTestItem.InputF1 = (decimal)temp[dicResults[i].index].F1 * 1000 * 1000;//MHZ->HZ
                        cPIMTestItem.InputF2 = (decimal)temp[dicResults[i].index].F2 * 1000 * 1000;
                        cPIMTestItem.OutputF = (decimal)temp[dicResults[i].index].Im_F * 1000 * 1000;
                        cPIMTestItem.HighSpec = (decimal)f2;
                        cPIMTestItem.TestValue = (decimal)dicResults[i].fValue;
                        cPIMTestItem.IsPass = dicResults[i].sPass;
                        _lstPIMTestItem.Add(cPIMTestItem);
                        #endregion

                        #region ZTE-ITEM

                        for (int j = 0; j < dicResults[i].valus.Length; j++)
                        {
                            var cParam = new tb_Param_DFL_Info();
                            cParam.resultid = 0;
                            cParam.partcode = productObj.sProductSN;
                            //cParam.Testequipid = TFPID;
                            cParam.Testequipid = "MOBI_" + productObj.sProcode.Substring(1, 2) + "_PIM_V1.6";

                            cParam.ProcessNum = PROCESSNUM;
                            cParam.UnitName = "通道" +
                                              lstConifg[i].ProCH + " PIM " +
                                              dicResults[i].valus[j].F1 + "-" +
                                              dicResults[i].valus[j].F2 + "MHz";
                            cParam.testtime = Convert.ToDateTime(dicResults[i].valus[j].Time_now);
                            cParam.ParamName = lstConifg[i].ProCH + "互调" +
                                               dicResults[i].valus[j].F1 + "-" +
                                               dicResults[i].valus[j].F2 + "MHz";
                            if (dicResults[i].sPass == GDefine.ContentPass2)
                            {
                                cParam.Res = 0;
                                sItemresult += "0";
                            }
                            else
                            {
                                cParam.Res = 1;
                                sItemresult += "1";
                            }

                            cParam.ResValue = dicResults[i].valus[j].Im_V.ToString();
                            cParam.ResDesc = lstConifg[i].SpecReg;
                            sItemselinfo += "1";
                            _lstParam.Add(cParam);
                        }

                        #region 敲击点频
                        //if (dicResults[i].valus_knock == null) continue;
                        //for (int j = 0; j < dicResults[i].valus_knock.Length; j++)
                        //{//敲击点频
                        //    var cParam = new tb_Param_DFL_Info();
                        //    cParam.resultid = 0;
                        //    cParam.partcode = productObj.sProductSN;
                        //    cParam.Testequipid = TFPID;
                        //    cParam.ProcessNum = PROCESSNUM;
                        //    cParam.UnitName = lstConifg[i].ProCH + " PIM " +
                        //                      dicResults[i].valus_knock[j].F1 + "-" +
                        //                      dicResults[i].valus_knock[j].F2 + "MHz性能测试";
                        //    cParam.testtime = timeStop;
                        //    cParam.ParamName = lstConifg[i].ProCH + "互调" +
                        //                       dicResults[i].valus_knock[j].F1 + "-" +
                        //                       dicResults[i].valus_knock[j].F2 + "MHz性能测试";
                        //    if (PIMFunction.compareSpec(dicResults[i].valus_knock[j].Im_V, lstConifg[i].SpecReg))
                        //    {
                        //        cParam.Res = 0;
                        //        sItemresult += "0";
                        //    }
                        //    else
                        //    {
                        //        cParam.Res = 1;
                        //        sItemresult += "1";
                        //    }

                        //    cParam.ResValue = dicResults[i].valus_knock[j].Im_V.ToString();
                        //    cParam.ResDesc = lstConifg[i].SpecReg;
                        //    sItemselinfo += "1";
                        //    _lstParam.Add(cParam);
                        //}
                        #endregion

                        #endregion
                    }
                    else
                    {//数据不全, 不上传
                        return false;
                    }
                }

                #region MOBI-PI
                _cPIMTest.ProductSN = productObj.sProductSN;
                _cPIMTest.TestTime = timeStop;
                _cPIMTest.ProductTypeID = productObj.sProcode;
                _cPIMTest.UserID = userObj.sUserID;
                _cPIMTest.PCIP = userObj.sEquID;
                if (_instr == InstrumentGroup.RFDESKTOP)
                {
                    _cPIMTest.SignalSource1 = "Rflight";
                    _cPIMTest.SignalSource2 = "Rflight";
                    _cPIMTest.SpectrumAnalyzer = "Rflight";
                }
                else
                {
                    _cPIMTest.SignalSource1 = "Jointcom";
                    _cPIMTest.SignalSource2 = "Jointcom";
                    _cPIMTest.SpectrumAnalyzer = "Jointcom";
                }

                _cPIMTest.TestStep = productObj.sPIMTestStep;
                _cPIMTest.IsPass = m_isAllFail ? GDefine.ContentFail2 : GDefine.ContentPass2;
                _cPIMTest.MoreContent = productObj.sConfigVer;
                #endregion

                #region ZTE-TI
                TimeSpan elapsedSpan = new TimeSpan(timeStop.Ticks - timeStart.Ticks);
                _cResult.resultid = 0;
                _cResult.partcode = productObj.sProductSN;
                _cResult.testtime = timeStart;
                _cResult.testperiod = (int)elapsedSpan.TotalSeconds;
                _cResult.testequipid = "jointcom";
                _cResult.projname = PROJNAME;
                _cResult.projver = PROJVER;
                _cResult.tfpid = TFPID;
                _cResult.userid = userObj.sUserID;
                _cResult.totalresult = m_isAllFail ? "1" : "0";
                _cResult.itemresult = sItemresult;
                _cResult.itemselinfo = sItemselinfo;
                _cResult.itemselcnt = lstConifg.Count;
                _cResult.itemcnt = lstConifg.Count;
                _cResult.remark = "";
                _cResult.IsPIM = true;
                #endregion

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("construct失败: " + ex.Message);
                return false;
            }
        }


        public List<PIMTestItemInfo> GetCurrentTestinfo(DateTime timeStop,
                                Dictionary<int, JcPIMRecordResult> dicResults,
                                List<ClsDataConfig> lstConifg,
                                ClsDataProduct productObj,
                                ClsDataSetup setupObj)
        {
            _lstPIMTestItem = new List<PIMTestItemInfo>();
            try
            {
                for (int i = 0; i < lstConifg.Count; i++)
                {
                    if (dicResults.ContainsKey(i))
                    {
                        double f1 = 0, f2 = 0;
                        PIMFunction.formateSpec(lstConifg[i].SpecReg, ref f1, ref f2);

                        #region MOBI-LIST
                        var cPIMTestItem = new PIMTestItemInfo();
                        cPIMTestItem.ProductSN = productObj.sProductSN;
                        cPIMTestItem.TestTime = timeStop;
                        cPIMTestItem.TopoAlias = lstConifg[i].ProCH;

                        if (setupObj.IsCheckOn)
                            cPIMTestItem.TopoAlias += "@(" + lstConifg[i].Check + ")";

                        cPIMTestItem.InterceptOrder = lstConifg[i].PIMOrder;
                        JcPIMDataResults[] temp = dicResults[i].valus_knock == null ? dicResults[i].valus : dicResults[i].valus_knock;
                        cPIMTestItem.InputPower1 = (decimal)temp[dicResults[i].index].P1;
                        cPIMTestItem.InputPower2 = (decimal)temp[dicResults[i].index].P2;
                        cPIMTestItem.InputF1 = (decimal)temp[dicResults[i].index].F1 * 1000 * 1000;//MHZ->HZ
                        cPIMTestItem.InputF2 = (decimal)temp[dicResults[i].index].F2 * 1000 * 1000;
                        cPIMTestItem.OutputF = (decimal)temp[dicResults[i].index].Im_F * 1000 * 1000;
                        cPIMTestItem.HighSpec = (decimal)f2;
                        cPIMTestItem.TestValue = (decimal)dicResults[i].fValue;
                        cPIMTestItem.IsPass = dicResults[i].sPass;
                        _lstPIMTestItem.Add(cPIMTestItem);
                        #endregion

                    }
                    else
                    {//数据不全, 不上传
                        return null;
                    }
                }


                return _lstPIMTestItem;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("construct失败: " + ex.Message);
                return null;
            }
        }

        public UpdatatestInfo UPdataTi(bool m_isAllFail, DateTime timeStart, DateTime timeStop,
                              List<ClsDataConfig> lstConifg,
                              ClsDataProduct productObj,
                              ClsDataUser userObj, string testband, bool flag)
        {
            UpdatatestInfo ti = new UpdatatestInfo();
            procode = productObj.sProcode;

            string sItemresult = "";
            string sItemselinfo = "";

            try
            {
                #region MOBI-PI
                ti.ProductSN = productObj.sProductSN;
                ti.TestTime = timeStop;
                ti.ProductTypeID = productObj.sProcode;
                ti.UserID = userObj.sUserID;
                ti.PCIP = userObj.sEquID;
                if (_instr == InstrumentGroup.RFDESKTOP)
                {
                    ti.SignalSource1 = "Rflight";
                    ti.SignalSource2 = "Rflight";
                    ti.SpectrumAnalyzer = "Rflight";
                }
                else
                {
                    ti.SignalSource1 = "Jointcom";
                    ti.SignalSource2 = "Jointcom";
                    ti.SpectrumAnalyzer = "Jointcom";
                }
                ti.TestStep = productObj.sPIMTestStep;
                ti.IsPass = m_isAllFail ? GDefine.ContentFail2 : GDefine.ContentPass2;
                ti.MoreContent = productObj.sConfigVer;
                ti.TestBand = testband;
                ti.Flag = flag;
                #endregion


                return ti;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("construct失败: " + ex.Message);
                return null;
            }
        }

        public void AddData(List<PIMTestItemInfo> iteminfo)
        {
            _lstPIMTestItem.AddRange(iteminfo);
        }


        public bool upload_MOBI(ModTestMode mode = ModTestMode.First)
        {
            if (mode == ModTestMode.Second)
            {
                return PIMData_SecondTest_Upload(_cPIMTest, _lstPIMTestItem);
            }
            else
            {
                return PIMData_Upload(_cPIMTest, _lstPIMTestItem);
            }           
        }

        public bool upload_ZTE()
        {
            return ZTE_PIMTestData_Upload(procode, _cResult, _lstParam);
        }

    }
}
